﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Model;
using Utility;
namespace Service
{
    /// <summary>
    /// 功能：HW_Notice类数据访问
    /// 创建人：Wilson
    /// 创建时间：2013-1-30   
    /// </summary>
    public class HW_NoticeService : IDisposable
    {
        #region 公共
        private System.ComponentModel.Component component = new System.ComponentModel.Component();

        private bool disposed = false;

        ~HW_NoticeService()
        {
            Dispose(false);
        }

        #endregion

        #region IDisposable 成员

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            if (!this.disposed)
            {

                if (disposing)
                {

                    component.Dispose();
                }

                disposed = true;

            }
        }
        #endregion
		#region##MapperHW_Notice
        public IList<HW_Notice> MapperList(SqlDataReader reader)
         {
            IList<HW_Notice> list = new List<HW_Notice>();
            HW_Notice obj=null;
            while (reader.Read())
            {
                obj = new HW_Notice();
                obj.NoticeCode = (int)reader["NoticeCode"];
                obj.NoticeTiltle = reader["NoticeTiltle"] as string;
                obj.NoticeContent = reader["NoticeContent"] as string;
                obj.CreateDate = (DateTime)reader["CreateDate"];
                obj.StartDate = reader["StartDate"] as DateTime?;
                obj.EndDate = reader["EndDate"] as DateTime?;
                obj.Statute = (bool)reader["State"];
                list.Add(obj);
            }
            return list;
         }
        public HW_Notice Mapper(SqlDataReader reader)
         {
            HW_Notice obj=null;
            if (reader.Read())
            {
                obj = new HW_Notice();
                obj.NoticeCode = (int)reader["NoticeCode"];
                obj.NoticeTiltle = reader["NoticeTiltle"] as string;
                obj.NoticeContent = reader["NoticeContent"] as string;
                obj.CreateDate = (DateTime)reader["CreateDate"];
                    obj.StartDate = reader["StartDate"] as DateTime?;
                    obj.EndDate = reader["EndDate"] as DateTime?;
                obj.Statute = (bool)reader["Statute"];
            }
            return obj;
         }
        #endregion
        #region##通过主键ID得到HW_Notice
        /// <summary>
        /// 功能：通过ID得到HW_Notice
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30   
        /// </summary>
        /// <param name="">主键ID</param>
        /// <returns></returns>
        public HW_Notice GetById(int noticecode)
        {            
            string sql = "select * from HW_Notice where NoticeCode = @PK";
            SqlParameter[] sp = new SqlParameter[]
            {
                new SqlParameter("@PK",4)
            };
            
            SqlDataReader reader=SqlHelper.ExecuteReader(WebConn.connString,CommandType.Text,sql,sp);
            return Mapper(reader);
        }
        #endregion
		
		#region##通过主键ID删除HW_Notice
        /// <summary>
        /// 功能：通过主键ID删除HW_Notice
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30   
        /// </summary>
        /// <param name="">主键ID</param>
        /// <returns></returns>
		public int DeleteById(int noticecode)
		{
			int i = 0;
			string sql = "delete from HW_Notice where NoticeCode = @PK";
            SqlParameter[] sp = new SqlParameter[]
            {
                new SqlParameter("@PK",4)
            };
            i= SqlHelper.ExecuteNonQuery(WebConn.connString,CommandType.Text,sql,sp);
			return i;
		}
        /// <summary>
        /// 功能：通过主键ID删除HW_Notice
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30   
        /// </summary>
        /// <param name="">主键ID</param>
        /// <returns></returns>
		public int DeleteById(SqlTransaction transaction,int noticecode)
		{
			int i = 0;
			string sql = "delete from HW_Notice where NoticeCode = @PK";
            SqlParameter[] sp = new SqlParameter[]
            {
                new SqlParameter("@PK",4)
            };
            i= SqlHelper.ExecuteNonQuery(transaction,CommandType.Text,sql,sp);
			return i;
		}
		#endregion			
		
        #region##添加HW_Notice
        /// <summary>
        /// 功能：添加HW_Notice
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int Add(HW_Notice hw_notice)
        {
            int result = 0;
            string sql = "insert into HW_Notice(NoticeTiltle,NoticeContent,CreateDate,StartDate,EndDate,State) values(@NoticeTiltle,@NoticeContent,@CreateDate,@StartDate,@EndDate,@Statute)";			
            SqlParameter[] sp = new SqlParameter[]
            {
				new SqlParameter("@NoticeTiltle",hw_notice.NoticeTiltle),
				new SqlParameter("@NoticeContent",hw_notice.NoticeContent),
				new SqlParameter("@CreateDate",hw_notice.CreateDate),
				new SqlParameter("@StartDate",hw_notice.StartDate),
				new SqlParameter("@EndDate",hw_notice.EndDate),
				new SqlParameter("@Statute",hw_notice.Statute)
            };
            result = SqlHelper.ExecuteNonQuery(WebConn.connString,CommandType.Text,sql,sp);
            return result;
        }
         /// <summary>
        /// 功能：添加HW_Notice
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int Add(SqlTransaction transaction,HW_Notice hw_notice)
        {
            int result = 0;
            string sql = "insert into HW_Notice(NoticeCode,NoticeTiltle,NoticeContent,CreateDate,StartDate,EndDate,Statute) values(@NoticeCode,@NoticeTiltle,@NoticeContent,@CreateDate,@StartDate,@EndDate,@Statute)";			
            SqlParameter[] sp = new SqlParameter[]
            {
				new SqlParameter("@NoticeCode",hw_notice.NoticeCode),
				new SqlParameter("@NoticeTiltle",hw_notice.NoticeTiltle),
				new SqlParameter("@NoticeContent",hw_notice.NoticeContent),
				new SqlParameter("@CreateDate",hw_notice.CreateDate),
				new SqlParameter("@StartDate",hw_notice.StartDate),
				new SqlParameter("@EndDate",hw_notice.EndDate),
				new SqlParameter("@Statute",hw_notice.Statute)
            };
            result = SqlHelper.ExecuteNonQuery(transaction,CommandType.Text,sql,sp);
            return result;
        }
        #endregion
		
		#region##得到HW_Notice集合
		/// <summary>
        /// 功能：得到HW_Notice集合
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30
        /// </summary>       
        /// <returns></returns>
		public IList<HW_Notice> GetList()
		{
			string sql = "select * from HW_Notice";			
			SqlDataReader reader = SqlHelper.ExecuteReader(WebConn.connString,CommandType.Text,sql);
            return MapperList(reader);
		}
        public IList<HW_Notice> GetList(int pageIndex,int pageSize,out int count)
        {
            string sql = "select top(@pageSize) * from HW_Notice where NoticeCode not in(select top(@pageSize*@pageIndex) NoticeCode from HW_Notice order by NoticeCode desc) order by NoticeCode desc";
            SqlParameter[] sp = new SqlParameter[]
            {
                new SqlParameter("@pageSize",pageSize),
                new SqlParameter("@pageIndex",pageIndex),
            };
            SqlDataReader reader = SqlHelper.ExecuteReader(WebConn.connString, CommandType.Text, sql, sp);

            string sql2 = "select count(*) from HW_Notice";
            object o = SqlHelper.ExecuteScalar(WebConn.connString, CommandType.Text, sql2);
            count = Convert.ToInt32(o);
            return MapperList(reader);
        }
		#endregion
		
		#region##修改HW_Notice
        /// <summary>
        /// 功能：修改HW_Notice
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int ModHW_Notice(HW_Notice hw_notice)
        {
            int result = 0;
            string sql = "update HW_Notice set NoticeCode = @NoticeCode,NoticeTiltle = @NoticeTiltle,NoticeContent = @NoticeContent,CreateDate = @CreateDate,StartDate = @StartDate,EndDate = @EndDate,Statute = @Statute where NoticeCode = @NoticeCode";
            SqlParameter[] sp = new SqlParameter[]
            {       
				new SqlParameter("@NoticeCode",hw_notice.NoticeCode),
				new SqlParameter("@NoticeTiltle",hw_notice.NoticeTiltle),
				new SqlParameter("@NoticeContent",hw_notice.NoticeContent),
				new SqlParameter("@CreateDate",hw_notice.CreateDate),
				new SqlParameter("@StartDate",hw_notice.StartDate),
				new SqlParameter("@EndDate",hw_notice.EndDate),
				new SqlParameter("@Statute",hw_notice.Statute)
            };
            result = SqlHelper.ExecuteNonQuery(WebConn.connString,CommandType.Text,sql,sp);
            return result;
        }
         /// <summary>
        /// 功能：修改HW_Notice
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int ModHW_Notice(SqlTransaction transaction,HW_Notice hw_notice)
        {
            int result = 0;
            string sql = "update HW_Notice set NoticeCode = @NoticeCode,NoticeTiltle = @NoticeTiltle,NoticeContent = @NoticeContent,CreateDate = @CreateDate,StartDate = @StartDate,EndDate = @EndDate,Statute = @Statute where NoticeCode = @NoticeCode";
            SqlParameter[] sp = new SqlParameter[]
            {       
				new SqlParameter("@NoticeCode",hw_notice.NoticeCode),
				new SqlParameter("@NoticeTiltle",hw_notice.NoticeTiltle),
				new SqlParameter("@NoticeContent",hw_notice.NoticeContent),
				new SqlParameter("@CreateDate",hw_notice.CreateDate),
				new SqlParameter("@StartDate",hw_notice.StartDate),
				new SqlParameter("@EndDate",hw_notice.EndDate),
				new SqlParameter("@Statute",hw_notice.Statute)
            };
            result = SqlHelper.ExecuteNonQuery(transaction,CommandType.Text,sql,sp);
            return result;
        }
        #endregion
		
		#region##统计HW_Notice
		/// <summary>
        /// 修改HW_Notice
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
		public int SelectCount()
		{
			int i = 0;
            string sql = "select count(*) from HW_Notice";
            i = (int)SqlHelper.ExecuteScalar(WebConn.connString,CommandType.Text,sql);
            return i;
		}
		/// <summary>
        /// 修改HW_Notice
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int SelectCountById()
        {
            int i=0;
            string sql="select count(NoticeCode) from HW_Notice";
            i = (int)SqlHelper.ExecuteScalar(WebConn.connString,CommandType.Text,sql);
            return i;
        }
		#endregion
        #region##自定义HW_Notice
        public int Send(int id)
        {
            string sql = "update HW_Notice set State='true' where NoticeCode=@id";
            SqlParameter[] sp = new SqlParameter[]
            {
                new SqlParameter("@id",id)
            };
            object i = SqlHelper.ExecuteNonQuery(WebConn.connString, CommandType.Text, sql, sp);
            return Convert.ToInt32(i);
        }
        #endregion
                
    }
}
